from com.nasgektw.cards.client.model.Configuration import Configuration
import random, com.nasgektw.cards.client.gui.WxUI
class DeckGeneratorByDeck(object):
    def __init__(self, deckName, imageFolderName, size, cardsNumber):
        self.deckName = deckName
        self.imageFolderName = imageFolderName
        self.size = size
        self.cardsNumber = cardsNumber
    def generate(self):
        cards = []
        deck = []
        for card in Configuration.cards:
            if self.deckName in card.decks:
                cards.append(card)
        files = com.nasgektw.cards.client.gui.WxUI.listFiles(self.imageFolderName)
        for i in range(self.size):
            if i % (self.size / self.cardsNumber) == 0 and cards:
                deck.append(random.choice(cards))
            elif files:
                deck.append(random.choice(files))
        return deck
class DeckGeneratorRandomly(object):
    def __init__(self, categories, types, imageFolderName, size, cardsNumber):
        self.categories = categories
        self.types = types
        self.imageFolderName = imageFolderName
        self.size = size
        self.cardsNumber = cardsNumber
    def generate(self):
        cards = {}
        deck = []
        wantedTypes = []
        typesRatio = []
        for cardType in self.types:
            if cardType[1]:
                wantedTypes.append(cardType[0])
        for card in Configuration.cards:
            if card.type in wantedTypes and not [category for category in card.categories if category not in self.categories]:
                if not cards.has_key(card.type):
                    cards[card.type] = []
                cards[card.type].append(card)
        for cardType in self.types:
            if cards.has_key(cardType[0]):
                for i in range(cardType[1]):
                    typesRatio.append(cardType[0])
        files = com.nasgektw.cards.client.gui.WxUI.listFiles(self.imageFolderName)
        for i in range(self.size):
            if i % (self.size / self.cardsNumber) == 0 and cards:
                deck.append(random.choice(cards[typesRatio[random.choice(range(len(typesRatio)))]]))
            elif files:
                deck.append(random.choice(files))
        return deck